import ModbusTCPClient from './modbus_tcp';
import { EnumDataType } from './enum';
import { Register } from './interface';

/**
 * 寄存器
 */
let registerArr: Register[] = [
    // { register: "000100", dataType: EnumDataType.Bit },
    // { register: "400100", dataType: EnumDataType.SwappedDouble },
    // { register: "400100", dataType: EnumDataType.Binary },
    // { register: "400100", dataType: EnumDataType.Int },
    // { register: "400100", dataType: EnumDataType.Decimal },
    { register: '400100', dataType: EnumDataType.Float },
]
/**
 * 读取一次断开的例子
 */
async function example1() {
    let modbusTcpClient = new ModbusTCPClient({ host: '10.211.55.15', port: 502, deviceID: 1 })   
    let result = await modbusTcpClient.setRegisters(registerArr).readOnce();
    console.log(JSON.stringify(result.get('400100')));
}
// example1()

/**
 * 轮询的例子
 */
async function example2() {
    let modbusTcpClient = new ModbusTCPClient({ host: '10.211.55.15', port: 502, deviceID: 1 })
    let result = await modbusTcpClient.setRegisters(registerArr).polling();
    console.log(result);
}
// example2()

/**
 * 写数据的例子
 */
async function example3() {
    let modbusTcpClient = new ModbusTCPClient({ host: '10.211.55.15', port: 502, deviceID: 1 })
    let result = await modbusTcpClient.setRegisters(registerArr).writeData('400100', 12.54);
    console.log(result);
}
example3()